package com.compty.paint.web.member;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.compty.core.page.Page;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.Bid;
import com.compty.paint.domain.Goods;
import com.compty.paint.domain.PromotionDetail;
import com.compty.paint.manager.BidManager;
import com.compty.paint.manager.GoodsManager;
import com.compty.paint.manager.PromotionDetailManager;
import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * @Todo:拍品推广记录Action(针对渠道商)
 * @author: 朱庆辉
 * @CreateTime:2012-4-16 上午01:22:48
 */
public class PromotionDetailAction  extends BaseAction<PromotionDetail>{
	
	private static final long serialVersionUID = -8114909061610465755L;
	
	private static Log logger = LogFactory.getLog(PromotionDetailAction.class);
	
	private PromotionDetailManager promotionDetailManager;
	private BidManager bidManager;
	private GoodsManager goodsManager;
	
	private Date startDate;
	private Date endDate;
	
	/***** 推广记录列表  *********/
	@SuppressWarnings("unchecked")
	public String list(){
		
		Object user_obj =  session.getAttribute(Constants.Userinfo);
		User userInfo = user_obj !=  null ? (User)user_obj:null;

        this.page =  promotionDetailManager.queryForPage(curPage, pageSize,userInfo,startDate,endDate,filterTxt,filterValue);
        List list = (List)page.getResult();
        
        for (int i = 0; i < list.size(); i++) {
        	
        	PromotionDetail promotionDetail =  (PromotionDetail) list.get(i);
        	
        	Long bid_id = promotionDetail.getBid_id();
        	if( bid_id != null){
        		Bid bid = bidManager.findById(bid_id);
        		promotionDetail.setBid(bid);
        		
            	Integer goods_id = bid.getGoods_id();
            	if(goods_id != null){
            		Goods goods = goodsManager.findById(goods_id);
            		bid.setGoods(goods);
            	}
        	}
		}
        
        logger.debug("query promotionDetail by page success.");
		
		return "list";
	}
	
	/*********** 获取拍品列表  *************/
	@SuppressWarnings("unchecked")
	public String fetch(){
		
		int pageSize = 30;
        Page page =  bidManager.queryBidForPage(curPage, pageSize,null,startDate,endDate,filterTxt,filterValue,null);
        List list1 = (List)page.getResult();
        for ( int i = 0; i < list1.size(); i++ ) {
        	Bid bid =  (Bid) list1.get(i);
        	
        	Integer goods_id = bid.getGoods_id();
        	if( goods_id != null){
        		Goods goods = goodsManager.findById(goods_id);
        		bid.setGoods(goods);
        	}
		}
        request.setAttribute("list1", list1);
        request.setAttribute("curPage1", page.getPageNo());
        request.setAttribute("pageCount1", page.getPageCount());
        
        page =  bidManager.queryBidForPage(curPage, pageSize,null,startDate,endDate,filterTxt,filterValue,null);
        List list2 = (List)page.getResult();
        for ( int i = 0; i < list2.size(); i++ ) {
        	Bid bid =  (Bid) list2.get(i);
        	
        	Integer goods_id = bid.getGoods_id();
        	if( goods_id != null){
        		Goods goods = goodsManager.findById(goods_id);
        		bid.setGoods(goods);
        	}
		}
        request.setAttribute("list2", list2);
        request.setAttribute("curPage2", page.getPageNo());
        request.setAttribute("pageCount2", page.getPageCount());
        
        page =  bidManager.queryBidForPageByCatalogType(curPage, pageSize,null,1,startDate,endDate,filterTxt,filterValue);
        List list3 = (List)page.getResult();
        for ( int i = 0; i < list3.size(); i++ ) {
        	Bid bid =  (Bid) list3.get(i);
        	
        	Integer goods_id = bid.getGoods_id();
        	if( goods_id != null){
        		Goods goods = goodsManager.findById(goods_id);
        		bid.setGoods(goods);
        	}
		}
        request.setAttribute("list3", list3);
        request.setAttribute("curPage3", page.getPageNo());
        request.setAttribute("pageCount3", page.getPageCount());
        
        page =  bidManager.queryBidForPageByCatalogType(curPage, pageSize,null,2,startDate,endDate,filterTxt,filterValue);
        List list4 = (List)page.getResult();
        for ( int i = 0; i < list4.size(); i++ ) {
        	Bid bid =  (Bid) list4.get(i);
        	
        	Integer goods_id = bid.getGoods_id();
        	if( goods_id != null){
        		Goods goods = goodsManager.findById(goods_id);
        		bid.setGoods(goods);
        	}
		}
        request.setAttribute("list4", list4);
        request.setAttribute("curPage4", page.getPageNo());
        request.setAttribute("pageCount4", page.getPageCount());
        
		return "fetch_list";
	}
	
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public void setPromotionDetailManager(PromotionDetailManager promotionDetailManager) {
		this.promotionDetailManager = promotionDetailManager;
	}

	public void setBidManager(BidManager bidManager) {
		this.bidManager = bidManager;
	}

	public void setGoodsManager(GoodsManager goodsManager) {
		this.goodsManager = goodsManager;
	}

}
